local LIB, REVISION = "LibDisplayAssist-1.2", 0
if not (LibStub and LibStub('CallbackHandler-1.0', true)) then
	error(LIB .. " requires LibStub and CallbackHandler-1.0", 0)
end

local lib, oldRevision = LibStub:NewLibrary(LIB, REVISION)
if not lib then return end

oldRevision = oldRevision or -1

local strsplit, tonumber, type = strsplit, tonumber, type

local GetCVar = GetCVar

--[[-----------------------------------------------------------------------------
Internal support
-------------------------------------------------------------------------------]]
lib.CBH = lib.CBH or LibStub('CallbackHandler-1.0'):New(lib, nil, nil, false)

lib.CVars = lib.CVars or { gxMaximize = true, gxWindow = true, uiscale = true, useUiScale = true }

if not lib.frame then
	lib.frame = CreateFrame('Frame', nil, UIParent)
	lib.frame:SetAllPoints(UIParent)

	lib.frame:SetScript('OnUpdate', function(self)											-- Always runs upon start/reload
		lib.resolution = GetCVar('gxResolution')
		lib.CBH:Fire("Update", self:GetWidth(), self:GetHeight(), lib:GetResolutionInfo())
		self:Hide()
	end)

	hooksecurefunc('SetCVar', function(name, value)											-- Monitor CVar changes during normal play
		if not lib.CVars[name] then return end
		lib.frame:Show()
	end)

	hooksecurefunc('SetScreenResolution', function(value)									-- In case a setting combo doesn't trigger a CVar change
		if lib.resolution == GetCVar('gxResolution') then return end
		lib.frame:Show()
	end)
end

lib.resolution = lib.resolution or GetCVar('gxResolution')

--[=[----------------------------------------------------------------------------

LIB:GetResolutionInfo(resolution)

	Take a string representing a screen resolution and break it down into useful
	information.

Input:

	resolution	(string) 	The resolution to break down on nil to use the current
									resolution.  Example: 1024x768

Returns:

	width			(number)		The resolution width.

	height		(number)		The resolution height.

	isWide		(boolean)	Whether or not the resolution is wide-screen.

------------------------------------------------------------------------------]=]
lib.GetResolutionInfo = lib.GetResolutionInfo or function(self, resolution)
	resolution = resolution or lib.resolution
	if type(resolution) ~= 'string' then
		error(LIB .. ":GetResolutionInfo(resolution) - 'resolution' expected string or nil, got " .. type(resolution), 2)
	end
	local width, height = strsplit('x', resolution)
	width, height = tonumber(width), tonumber(height)
	if not (width and height) then
		error(LIB .. ":GetResolutionInfo(resolution) - 'resolution' is not in the proper format", 2)
	end
	return width, height, height / width < 0.75
end

--[=[----------------------------------------------------------------------------

LIB:Register(nameSpace, func [, arg])

	Register for a callback on the "Update" event.

Input:

	nameSpace	(table)		The addon that will be receiving the callback.

	func			(function)	A function reference or method name within nameSpace.
					(string)

	arg			(any)			An optional arguement to pass during the callback.

------------------------------------------------------------------------------]=]
lib.Register = lib.Register or function(self, nameSpace, ...)
	self.RegisterCallback(nameSpace, "Update", ...)
end

--[=[----------------------------------------------------------------------------

LIB:Unregister(nameSpace)

	Unregister for the callback on the "Update" event.

Input:

	nameSpace	(table)		The addon that was receiving the callback.

------------------------------------------------------------------------------]=]
lib.Unregister = lib.Unregister or function(self, nameSpace)
	self.UnregisterCallback(nameSpace, "Update")
end
